package com.yyf.springboot2.Test;

import java.util.ArrayList;
import java.util.List;

public class TestQueue {

    public static void main(String[] args) {
        MyCircularQueue circularQueue = new MyCircularQueue(3);

        circularQueue.enQueue(1);  // 返回 true

        circularQueue.enQueue(2);  // 返回 true

        circularQueue.enQueue(3);  // 返回 true

        circularQueue.enQueue(4);  // 返回 false，队列已满

//        circularQueue.Rear();  // 返回 3

        circularQueue.isFull();  // 返回 true

        circularQueue.deQueue();  // 返回 true

        circularQueue.enQueue(4);  // 返回 true

//        circularQueue.Rear();  // 返回 4
    }




 static  class MyCircularQueue {
        private List<Integer> data;
        // a pointer to indicate the start position
        private int start;//头部指针
        private int tail;//尾部指针
        private int size;//长度

        /** Initialize your data structure here. Set the size of the queue to be k. */
        public  MyCircularQueue(int k) {
            this.data = new ArrayList<Integer>(k);
            this.size = k;
            this.start = 0;
            this.tail=0;
        }

        /** Insert an element into the circular queue. Return true if the operation is successful. */
        public  boolean enQueue(int value) {
            if((tail+1)%size== start){
                data.add(value);
                tail++;
                return true;
            }else{
                return false;
            }


        }

        /** Delete an element from the circular queue. Return true if the operation is successful. */
        public boolean deQueue() {
            if(data.isEmpty()){
                return false;
            }
            if( tail == start ){
                return false;
            }
            start++;
            return true;

        }

        /** Get the front item from the queue. */
//        public int Front() {
//            return data.get(p_start);
//        }

        /** Get the last item from the queue. */
//        public int Rear() {
//            return data.get(p_tail);
//        }

        /** Checks whether the circular queue is empty or not. */
        public boolean isEmpty() {
            if(start == tail){
                return true;
            }else{
                return false;
            }

        }

        /** Checks whether the circular queue is full or not. */
        public boolean isFull() {
            if((tail+1)%size==start){
                return true;
            }else{
                return false;
            }

        }
    }

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */




}

